package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.FileUtil;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.widget.MultiLinesViewNew;
import com.isunland.managesystem.widget.SingleLineViewNew;
import java.util.Date;

/**
 * 公告基类
 *
 * @author wu
 */
public class PublishAnnounceBaseFragment extends BaseFragment {

  protected static final int REQUEST_ACQUIRE_STARTTIME = 0;
  protected static final int REQUEST_ACQUIRE_FINISHTIME = 1;
  protected static final int REQUEST_ACQUIRE_ADDFILE = 2;
  //protected static final int REQUEST_APPLY_SCALE = 3;
  protected static final int REQUEST_KNOWLEDGE_TYPE = 4;
  protected static final int SAVE = 5;
  protected static final int SAVE_CHECK = 6;
  //public static final String EXTRA_STAFF = "com.isunland.managesystem.ui.EXTRA_STAFF";
  //public static final String EXTRA_STAFF_CODE = "com.isunland.managesystem.ui.EXTRA_STAFF_CODE";
  //public static final String EXTRA_DEPARTMENT = "com.isunland.managesystem.ui.EXTRA_DEPARTMENT";
  //public static final String EXTRA_DEPARTMENT_CODE = "com.isunland.managesystem.ui.EXTRA_DEPARTMENT_CODE";
  //
  //public static final String EXTRA_SCALE = "com.isunland.managesystem.ui.EXTRA_SCALE";
  //public static final String EXTRA_SCALE_NAME = "com.isunland.managesystem.ui.EXTRA_SCALE_NAME";
  //public static final String EXTRA_STAFF_DETAIL = "com.isunland.managesystem.ui.EXTRA_STAFF_DETAIL";

  protected SingleLineViewNew mtvAnnounceTypePublicAnnounce;// 公文类别
  protected SingleLineViewNew mtvKeyWordsDetailPublicAnnounce;// 关键字
  protected SingleLineViewNew mtv_announceTitle;// 公告标题
  protected MultiLinesViewNew mtv_announceIntroduction;// 公告简介
  /** 用于控制webview的容器,包含标题等 */
  protected View rlWebViewHolder;
  protected WebView mwv_announceDetail;
  protected MultiLinesViewNew mtvOhtersRemark;// 备注
  //	protected TextView mtv_beginDate;// 起始时间
  //	protected TextView mtv_endDate;// 截止日期
  protected SingleLineViewNew mtv_addFileAnnounce;// 附件
  protected SingleLineViewNew mtv_registerAnnounce;// 创建人
  protected SingleLineViewNew mtv_registerTimeAnnounce;// 创建时间
  protected SingleLineViewNew mtv_registerDepartment;// 创建部门
  protected CurrentUser mCurrentUser;
  //protected String mRegisteDate; // 登记日期
  protected String mRegister;// 登记人
  protected String mRegisterId;// 登记人Id
  protected String mDataStatus;// 数据状态
  protected String mStartTime;// 起始时间
  protected String mEndTime;// 截止时间
  protected String mChoseFileName;
  protected String mDate;
  protected String mMemberCode;

  //protected String mDepartment;
  //protected String mDepartmentCodes;
  //protected String mStaff;
  //protected String mStaffCodes;
  protected String mScale;
  protected String mScaleCode;
  protected String mJobNumber;
  protected String mFilePath;
  protected String mKnowledgeName;// 知识公告类别
  protected String mKnowledgeCode;// 知识公告类别编码
  protected String mId;// 本次记录id
  protected BaseVolleyActivity mActivity;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置返回图标

    mCurrentUser = CurrentUser.newInstance(getActivity());
    mRegister = mCurrentUser.getRealName();
    mRegisterId = mCurrentUser.getJobNumber();
    mMemberCode = mCurrentUser.getMemberCode();
    mScale = mCurrentUser.getMemberName();
    mScaleCode = mCurrentUser.getMemberCode();
    //noinspection deprecation
    mDate = MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_DATE);
    mJobNumber = mCurrentUser.getJobNumber();
    mActivity = (BaseVolleyActivity) getActivity();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_add_announce, container, false);
    mtvAnnounceTypePublicAnnounce =
        (SingleLineViewNew) view.findViewById(R.id.tv_announceType_publicAnnounce);
    mtvKeyWordsDetailPublicAnnounce =
        (SingleLineViewNew) view.findViewById(R.id.tv_keyWordsDetail_publicAnnounce);
    mtv_announceTitle = (SingleLineViewNew) view.findViewById(R.id.tv_announceTitle);
    mtv_announceIntroduction = (MultiLinesViewNew) view.findViewById(R.id.tv_announceIntroduction);
    mtvOhtersRemark = (MultiLinesViewNew) view.findViewById(R.id.tv_ohtersRemark);
    //		mtv_beginDate = (TextView) view.findViewById(R.id.tv_beginDate);
    //		mtv_endDate = (TextView) view.findViewById(R.id.tv_endDate);
    mtv_registerAnnounce = (SingleLineViewNew) view.findViewById(R.id.tv_registerAnnounce);
    mtv_registerTimeAnnounce = (SingleLineViewNew) view.findViewById(R.id.tv_registerTimeAnnounce);
    mtv_registerDepartment = (SingleLineViewNew) view.findViewById(R.id.tv_createDeprtment);
    mtv_addFileAnnounce = (SingleLineViewNew) view.findViewById(R.id.tv_addFileAnnounce);
    mwv_announceDetail = (WebView) view.findViewById(R.id.wv_announceDetail);
    rlWebViewHolder = view.findViewById(R.id.rl_wv_holder);

    //		mtv_beginDate.setText(MyDateUtil.getBeforeThreeMonth());
    //		mtv_endDate.setText(mDate);
    mtv_registerAnnounce.setTextContent(mRegister);
    mtv_registerTimeAnnounce.setTextContent(mDate);
    mtv_registerDepartment.setTextContent(mCurrentUser.getDeptName());
    //		mtv_beginDate.setOnClickContentListener(this);
    //		mtv_endDate.setOnClickContentListener(this);
    mtv_addFileAnnounce.setOnClickContentListener(new OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(REQUEST_ACQUIRE_ADDFILE);
      }
    });
    mtvAnnounceTypePublicAnnounce.setOnClickContentListener(new OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(REQUEST_KNOWLEDGE_TYPE);
      }
    });

    initView();
    return view;
  }

  protected void initView() {

  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   */
  public void showDialog(int requestCode) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_ACQUIRE_STARTTIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case REQUEST_ACQUIRE_FINISHTIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;

      case REQUEST_ACQUIRE_ADDFILE:
        dialog = new CompanyForumTopicFileDialogFragment();
        break;
      case REQUEST_KNOWLEDGE_TYPE:
        dialog = PublishKnowledgeTypeDialogFragment.newInstance(
            PublishKnowledgeTypeDialogFragment.PUBLISH_ANNOUNCE);
        break;
      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    if (dialog == null) return;
    dialog.setTargetFragment(PublishAnnounceBaseFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    //		// 开始时间
    //		if (requestCode == REQUEST_ACQUIRE_STARTTIME && data != null) {
    //			Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
    //			mStartTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
    //			mtv_beginDate.setText(mStartTime);
    //		}
    //		// 结束时间
    //		if (requestCode == REQUEST_ACQUIRE_FINISHTIME && data != null) {
    //			Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
    //			mEndTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
    //			getActivity().setResult(REQUEST_ACQUIRE_FINISHTIME);
    //			mtv_endDate.setText(mEndTime);
    //			return;
    //		}
    // 添加附件
    if (requestCode == REQUEST_ACQUIRE_ADDFILE && data != null) {
      mFilePath = data.getStringExtra(CompanyForumTopicFileDialogFragment.EXTRA_RESULT);

      LogUtil.i("mPath=====" + mFilePath);
      if (mFilePath != null) {
        mChoseFileName = FileUtil.getFileNameByFilePath(mFilePath);
        mtv_addFileAnnounce.setTextContent(mChoseFileName);
      }
    }
    //		// 适用范围
    //		if (requestCode == REQUEST_APPLY_SCALE && data != null) {
    //			mApply = data.getStringExtra(EXTRA_SCALE);
    //			mApplyName = data.getStringExtra(EXTRA_SCALE_NAME);
    //			mApplyData = data.getStringExtra(EXTRA_STAFF_DETAIL);
    //			mtv_applyScape.setText(mApplyName);
    //			LogUtil.i("mApply===" + mApply + "-----mApplyName===" + mApplyName + "----mApplyData===" + mApplyData);
    //		}
    // 公文类别
    if (requestCode == REQUEST_KNOWLEDGE_TYPE && data != null) {
      CustomerDialog dialog =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mKnowledgeName = dialog.getName();
      mKnowledgeCode = dialog.getId();
      mtvAnnounceTypePublicAnnounce.setTextContent(mKnowledgeName);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
